﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Commands;
using Module.Core.LoginSystem.Components.Modules;
using Telerik.WebControls;

namespace Web.Cms
{
    public partial class Default : AdminPage
    {
        private string _path = string.Empty;
        private CommandInfo _curCommand;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            string _ctrlKey = Request.QueryString["ctrlkey"];
            if (_ctrlKey != null)
            {
                if (_ctrlKey == "accessdenied")
                {
                    phModule.Controls.Add(LoadControl("../Cms/UserControls/AccessDenied.ascx"));
                    return;
                }
                if (CurrentCommand == null)
                {
                    Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "accessdenied", null, null));
                }
                else if (!CurrentUser.IsSuperUser && !CommandController.UserHasPermision(CurrentUser.UserID, CurrentCommand.CommandID))
                {
                    Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "accessdenied", null, null));
                }
                else
                {
                    ModuleInfo _module = ModuleController.GetModule(_ctrlKey);
                    AuthenticatedUserControl _curControl = LoadAuthenticatedModule(_module);

                    int _readAble;
                    int _writeAble;
                    CommandController.GetCommandPermisionForUser(CurrentUser.UserID, CurrentCommand.CommandID, out _readAble,
                                                                 out _writeAble);
                    if (_curControl != null)
                    {
                        _curControl.IsReadAble = _readAble > 0;
                        _curControl.IsWriteAble = _writeAble > 0;
                        //_curControl.IsReadAble = true;// _readAble > 0;
                        //_curControl.IsWriteAble = true;// _writeAble > 0;
                        phModule.Controls.Add(_curControl);
                    }
                }
            }
            else
            {
                phModule.Controls.Add(LoadControl("../Cms/UserControls/WellCome.ascx"));
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetPath();
                LoadSidebar();
            }
        }
        private void LoadSidebar()
        {
            radmMenu.Items.Clear();
            List<CommandInfo> _lstCommands = CommandController.GetCommands(this.CurrentPortalID, 0);
            foreach (CommandInfo _command in _lstCommands)
            {
                if (!_command.IsEnable || !_command.IsVisible) continue;
                if (!CurrentUser.IsSuperUser && !CommandController.UserHasPermision(CurrentUser.UserID, _command.CommandID)) continue;
                RadMenuItem _item = new RadMenuItem();
                _item.Text = _command.Title;
                _item.Value = _command.CommandID.ToString();
                if (_path.IndexOf("|" + _item.ID + "|") > -1)
                {
                    //_item.Expand = true;
                }
                if (_command.CtrlKey != string.Empty)
                {
                    _item.NavigateUrl = UIProcess.GetCPPage(CurrentPortalID, _command.CtrlKey, null, null);
                }
                if (_curCommand != null && _command.CommandID == _curCommand.CommandID)
                {
                    //_item.Selected = true;
                }
                radmMenu.Items.Add(_item);
                LoadSidebarItems(_item);
            }
        }
        private void LoadSidebarItems(RadMenuItem _curItem)
        {
            int _curCommandId = ConvertUtility.ToInt32(_curItem.Value);
            List<CommandInfo> _lstCommands = CommandController.GetCommands(this.CurrentPortalID, _curCommandId);
            foreach (CommandInfo _command in _lstCommands)
            {
                if (!_command.IsEnable || !_command.IsVisible) continue;
                if (!CurrentUser.IsSuperUser && !CommandController.UserHasPermision(CurrentUser.UserID, _command.CommandID)) continue;
                RadMenuItem _item = new RadMenuItem();
                _item.Text = _command.Title;
                _item.Value = _command.CommandID.ToString();
                if (_path.IndexOf("|" + _item.ID + "|") > -1)
                {
                    //_item.Expanded = true;

                }

                if (_command.CtrlKey != string.Empty)
                {
                    _item.NavigateUrl = UIProcess.GetCPPage(CurrentPortalID, _command.CtrlKey, null, null);
                }
                if (_curCommand != null && _command.CommandID == _curCommand.CommandID)
                {
                    //_item.Selected = true;
                }
                _curItem.Items.Add(_item);
                LoadSidebarItems(_item);
            }
        }

        private void GetPath()
        {
            _path = "|";
            string _ctrlKey = Request.QueryString["ctrlkey"];
            if (_ctrlKey != null)
            {
                _curCommand = CommandController.GetCommand(_ctrlKey);
                while (_curCommand != null)
                {
                    _path += _curCommand.CommandID + "|";
                    _curCommand = CommandController.GetCommand(_curCommand.ParentID);
                }
            }
        }   
    }
}